<?php
/**
 * Description: 获取事业单位整卷列表
 * User: lizengcai
 * Date: 16/12/1
 * Time: 10:30
 */

class GetEntirePapers implements iController {
    const AREAID = 1;               //地区id
    const PAGECOUNT = 15;           //每页显示条数
    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (!isset($queryArgs['page']) || !is_numeric($queryArgs['page']) || $queryArgs['page'] <= 0) {
            return genOutput(1000);
        }

        $sql = 'SELECT * FROM `institution_papers` WHERE `available`=1';

        if (empty($queryArgs['area_id']) && empty($queryArgs['year'])) { //列出推荐试卷

            $sql .= ' AND `area_id`='.self::AREAID.' ORDER BY `year` DESC LIMIT '.($queryArgs['page']-1) * self::PAGECOUNT.', '.self::PAGECOUNT;
            $papers = $cmsdb->getResults($sql);

            $list = array();
            $names = array();
            foreach ($papers as $p) {
                if (in_array($p->paper_name, $names, true)) //过滤名称相同的试卷
                    continue;

                $list[] = array(
                    'id' => intval($p->id),
                    'name' => $p->paper_name
                );
                $names[] = $p->paper_name;
            }

            return genOutput(1, array('list' => $list));

        } else {

            if (!empty($queryArgs['area_id']))
                $sql .= ' AND `area_id`='.intval($queryArgs['area_id']);

            if (!empty($queryArgs['year']))
                $sql .= ' AND `year`='.intval($queryArgs['year']);

            $sql .= ' ORDER BY `year`, `id` DESC LIMIT '.($queryArgs['page']-1) * self::PAGECOUNT.', '.self::PAGECOUNT;
            $papers = $cmsdb->getResults($sql);

            $list = array();
            $names = array();
            foreach ($papers as $p) {
                if (in_array($p->paper_name, $names, true)) //过滤名称相同的试卷
                    continue;

                $list[] = array(
                    'id' => intval($p->id),
                    'name' => $p->paper_name
                );
                $names[] = $p->paper_name;
            }

            return genOutput(1, array('list' => $list));
        }
    }

}
